package com.evaluationSystem.service.impl;

import com.evaluationSystem.entity.QuestionnaireWeight;
import com.evaluationSystem.entity.WeightData;
import com.evaluationSystem.entity.WeightList;
import com.evaluationSystem.entity.vo.WeightDataVo;
import com.evaluationSystem.exception.EvaluationException;
import com.evaluationSystem.mapper.WeightListMapper;
import com.evaluationSystem.service.WeightListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName WeightServiceImpl
 * @Description TODO
 * @Author yinzhenxing
 * @Date 2023/1/4 17:59
 **/
@Service
public class WeightListServiceImpl implements WeightListService {
    @Autowired
    private WeightListMapper weightListMapper;

    @Override
    public List<WeightList> getList() {
        return weightListMapper.getList();
    }

    @Override
    public void save(WeightList weight) {
        weightListMapper.save(weight);
    }

    @Override
    public void edit(WeightList weight) {
        weightListMapper.edit(weight);
    }

    @Override
    public List<WeightDataVo> getInfo(String questionId) {
        return weightListMapper.getInfo(questionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWeight(Map<String, Object> map) {
        String questionId = (String) map.get("questionId");
        map.remove("questionId");

        int sum = map.values().stream().mapToInt(num -> (Integer) num).sum();
        if (100 != sum) {
            throw new EvaluationException("权重之和必须为100");
        }

        QuestionnaireWeight questionnaireWeight = new QuestionnaireWeight();
        questionnaireWeight.setQuestionId(questionId);
        // 先添加关系表
        weightListMapper.saveWeightData(questionnaireWeight);

        // 获取权重列表绑定权重关系
        List<WeightList> list = weightListMapper.getList();

        List<WeightData> weightDataList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            int appraiseTiesId = Integer.parseInt(entry.getKey());
            WeightData weightData = new WeightData();
            // 取到刚刚添加关系表的 dataId
            weightData.setDataId(questionnaireWeight.getDataId());
            weightData.setAppraiseTiesId(appraiseTiesId);
            // 查找权重id所对应的权重关系
            WeightList weight = list.stream().filter(res -> res.getAppraiseTiesId() == appraiseTiesId).findFirst().orElse(null);
            if (weight != null) {
                weightData.setAppraiseTies(weight.getAppraiseTies());
            }
            weightData.setValued((Integer) entry.getValue());
            weightData.setQuestionId(questionId);
            weightDataList.add(weightData);
        }

        weightListMapper.saveWeightList(weightDataList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editWeight(Map<String, Object> map) {
        String questionId = (String) map.get("questionId");
        Integer dataId = (Integer) map.get("dataId");
        map.remove("questionId");
        map.remove("dataId");

        int sum = map.values().stream().mapToInt(num -> (Integer) num).sum();
        if (100 != sum) {
            throw new EvaluationException("权重之和必须为100");
        }

        Map<String, Object> delMap = new HashMap<>();
        delMap.put("dataIdList", Arrays.asList(dataId));

        // 先删除再添加
        weightListMapper.delWeightData(delMap);
        weightListMapper.delWeightList(delMap);

        QuestionnaireWeight questionnaireWeight = new QuestionnaireWeight();
        questionnaireWeight.setQuestionId(questionId);
        // 先添加关系表
        weightListMapper.saveWeightData(questionnaireWeight);

        // 获取权重列表绑定权重关系
        List<WeightList> list = weightListMapper.getList();

        List<WeightData> weightDataList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            int appraiseTiesId = Integer.parseInt(entry.getKey());
            WeightData weightData = new WeightData();
            // 取到刚刚添加关系表的 dataId
            weightData.setDataId(questionnaireWeight.getDataId());
            weightData.setAppraiseTiesId(appraiseTiesId);
            // 查找权重id所对应的权重关系
            WeightList weight = list.stream().filter(res -> res.getAppraiseTiesId() == appraiseTiesId).findFirst().orElse(null);
            if (weight != null) {
                weightData.setAppraiseTies(weight.getAppraiseTies());
            }
            weightData.setValued((Integer) entry.getValue());
            weightData.setQuestionId(questionId);
            weightDataList.add(weightData);
        }

        weightListMapper.saveWeightList(weightDataList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delWeight(Map<String, Object> map) {
        weightListMapper.delWeightData(map);

        weightListMapper.delWeightList(map);
    }

}
